﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.tools
{
    public partial class copy_category : Manage
    {
        public int channel_id;                                //栏目ID
        protected int pageSize = 20;                          //每页记录数
        protected int page;                                   //当前页
        protected int pcount;                                 //记录总数
        protected int category_id;                                //类别ID
        protected string keywords;                            //关键字
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);
            category_id = GetInt("category_id", 0);
            keywords = GetString("keywords");

            if (!IsPostBack)
            {
                chkLoginLevel("data_copy");
                ddlCategoryBind();
                if (this.category_id > 0)
                {
                    RptBind(this.category_id);
                }
            }
        }

        #region 组合SQL语句
        private string Comb_SqlTxt(int _category_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            strTemp.Append("id>0 ");
            if (_category_id > 0)
            {
                strTemp.Append(" and category_id in(select id from he_category where id=" + _category_id + ")");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 组合URL语句
        protected string Comb_UrlTxt(int _category_id, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (_category_id > 0)
            {
                strTemp.Append("category_id=" + _category_id.ToString() + "&");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + HttpContext.Current.Server.UrlEncode(_keywords) + "&");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 类别绑定
        private void ddlCategoryBind()
        {


            List<Model.Lang> lst = new He.BLL.config().LoadConfig<List<Model.Lang>>(LangXmlPath);

            string lang_list = "";
            foreach (Model.Lang item in lst)
            {
                if (item.IsShow && item.Value != "cn")
                {
                    lang_list = lang_list + "," + item.Value + ",";
                }
            }


            BLL.channel sbll = new BLL.channel();
            BLL.category cbll = new BLL.category();
            ddlFromCategoryId.Items.Add(new ListItem("请选择分类", ""));
            ddlToCategoryId.Items.Add(new ListItem("请选择分类", ""));
            //绑定中文类别
            DataTable he_sys = sbll.GetList("is_lock=0").Tables[0];
            foreach (DataRow dr_sys in he_sys.Rows)
            {
                string lang_ver = LangHelper.GetLangVer(dr_sys["lang"]);
                DataTable he_category = cbll.GetList(0, Convert.ToInt32(dr_sys["id"]));
                foreach (DataRow dr_category in he_category.Rows)
                {
                    if (dr_sys["lang"].ToString() == "cn")
                    {
                        string Id = dr_category["id"].ToString();
                        int layer = int.Parse(dr_category["layer"].ToString());
                        string Title = dr_category["title"].ToString();

                        if (layer == 1)
                        {
                            Title = string.Format("【{0}->{1}】{2}", lang_ver, dr_sys["manage_title"], Title);
                            ddlFromCategoryId.Items.Add(new ListItem(Title, Id));
                        }
                        else
                        {
                            Title = "├ " + Title;
                            Title = StringHelper.Repeat("　", layer - 1) + Title;
                            ddlFromCategoryId.Items.Add(new ListItem(Title, Id));
                        }
                    }

                    if (lang_list.Contains("," + dr_sys["lang"].ToString() + ","))
                    {
                        string Id = dr_category["id"].ToString();
                        int layer = int.Parse(dr_category["layer"].ToString());
                        string Title = dr_category["title"].ToString().Trim();

                        if (layer == 1)
                        {
                            Title = string.Format("【{0}->{1}】{2}", lang_ver, dr_sys["manage_title"], Title);
                            ddlToCategoryId.Items.Add(new ListItem(Title, Id));
                        }
                        else
                        {
                            Title = "├ " + Title;
                            Title = StringHelper.Repeat("　", layer - 1) + Title;
                            ddlToCategoryId.Items.Add(new ListItem(Title, Id));
                        }
                    }
                }
            }
        }
        #endregion

        #region 数据绑定
        private void RptBind(int _category_id)
        {
            if (this.category_id > 0)
            {
                this.ddlFromCategoryId.SelectedValue = this.category_id.ToString();
            }
            this.txtKeywords.Text = this.keywords;
            string strWhere = Comb_SqlTxt(this.category_id, this.keywords);

            //判断模块
            BLL.category cbll = new BLL.category();
            Model.category cmodel = cbll.GetModel(this.category_id);

            BLL.channel sbll = new BLL.channel();
            Model.channel smodel = sbll.GetModel(cmodel.channel_id);

            BLL.article abll = new BLL.article();
            rptList.DataSource = abll.GetPageList(pageSize, page, strWhere, "sort_id asc,id desc");
            rptList.DataBind();
            pcount = abll.GetRecordCount(strWhere);
        }
        #endregion

        //选择分类
        protected void ddlFromCategoryId_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _category_id;
            if (int.TryParse(this.ddlFromCategoryId.SelectedValue.ToString(), out _category_id))
            {
                Response.Redirect("copy_category.aspx?" + Comb_UrlTxt(_category_id, this.keywords) + "page=0");
            }
            else
            {
                Response.Redirect("copy_category.aspx?" + Comb_UrlTxt(0, this.keywords) + "page=0");
            }
        }
        //查询
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("copy_category.aspx?" + Comb_UrlTxt(this.category_id, txtKeywords.Text.Trim()) + "page=0");
        }
    }
}